package com.ddone.movie.controller;

import com.ddone.movie.bean.Movie;
import com.ddone.movie.dao.MovieDao;
import com.ddone.movie.service.MovieService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;
import java.util.UUID;

/**
 * @author ddone
 * @date 2024/3/22-17:40
 */
@Controller
@Slf4j
@RequestMapping("/movie")
public class MovieController {

    @Autowired
    private MovieService movieService;

    @GetMapping("/show/list")
    public String showList(Model model){
        List<Movie> movieList = movieService.getAll();
        model.addAttribute("movieList", movieList);
        return "movie/movie-list";
    }

    @GetMapping("/remove")
    public String deleteById(@RequestParam("movieId") String movieId){
        movieService.removeMovieById(movieId);
        List<Movie> all = movieService.getAll();
        return  "forward:show/list";
    }

    @GetMapping("/get")
    public String getById(@RequestParam("movieId") String movieId, Model model){
        Movie movie = movieService.getMovieById(movieId);
        model.addAttribute("movie", movie);
        return "movie/update";
    }

    @PostMapping("/update")
    public String update(Movie movie){
        movieService.updateMovie(movie);
        return "redirect:show/list";
    }

    @PostMapping("/save")
    public String save(Movie movie){
        movieService.saveMovie(movie);
        return "redirect:show/list";
    }

}
